using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.Machinery.Queries;

/// <summary>
/// 设备查询命令，包含产线和工位信息。
/// </summary>
/// <param name="LineId">产线 Id</param>
public sealed record GetMachineryListQuery(long? LineId) : IRequest<IResult<List<EquipMachineryDto>>>;

internal sealed class GetMachineryListQueryHandler(IRepository<EquipMachinery> repo) : IRequestHandler<GetMachineryListQuery, IResult<List<EquipMachineryDto>>>
{
    public async Task<IResult<List<EquipMachineryDto>>> Handle(GetMachineryListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .Includes(m => m.Line)
            .Includes(m => m.Workstation)
            .WhereIF(request.LineId > 0, m => m.LineId == request.LineId)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<EquipMachineryDto>>());
    }
}
